import { ElMessageBox, ElMessage } from 'element-plus';
// import 'element-plus/es/components/message-box/style';
import { markRaw } from 'vue';
import { Delete } from '@element-plus/icons-vue';

export const useUtils = () => {
  return {
    openDeleteMsgBox: (callBack: Function, icon?: any) => {
      let config = {};
      if (icon)
        config = {
          confirmButtonText: '确 认',
          cancelButtonText: '取 消',
          type: 'error',
          draggable: true,
          icon: markRaw(icon),
        };
      else
        config = {
          confirmButtonText: '确 认',
          cancelButtonText: '取 消',
          type: 'error',
          draggable: true,
          icon: markRaw(Delete),
        };
      ElMessageBox.confirm('您将永久地删除该学生信息,是否继续?', '提 示', config)
        .then(() => {
          callBack();
        })
        .catch(() => {
          ElMessage({
            type: 'success',
            message: '您取消了操作',
          });
        });
    },
    debounce: function (func: Function, wait = 2000, immediate = false) {
      let timeout: any;
      return () => {
        let context = this;
        let args = arguments;

        if (timeout) clearTimeout(timeout);
        if (immediate) {
          var callNow = !timeout;
          timeout = setTimeout(() => {
            timeout = null;
          }, wait);
          if (callNow) func.apply(context, args);
        } else {
          timeout = setTimeout(function () {
            func.apply(context, args);
          }, wait);
        }
      };
    },
  };
};
